home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT66.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  9.7 KB  |  376 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0
  4.                  Demonstration Program 66
  5.  
  6.  Uses VESA routines to create a large 256 color mouse cursor in any SVGA
  7.  mode.  The first loop will draw lines while you can move the mouse. Hit
  8.  a key and the lines will stop.  Hit another key to quit the program.
  9.  
  10.  *** PROJECT ***
  11.  This program requires the WGT5_WC.LIB and WVESA_WC.LIB files to be linked.
  12.  
  13.  *** DATA FILES ***
  14.  SVGACURS.SPR
  15.                                WATCOM C++ VERSION
  16. ==============================================================================
  17. */
  18.  
  19. #include <wgt5.h>
  20. #include <wgtvesa.h>
  21. #include <conio.h>
  22. #include <dos.h>
  23. #include <graph.h>
  24.  
  25.  
  26. int mousestate = 1;
  27. block mousebackground;          /* Stores what is behind the cursor */
  28. block mouse_image[10];          /* Holds the image(s) used for the cursor */
  29.                 /* You can animate the cursor by using more
  30.                    images in this array. */
  31. short ox, oy;                   /* Old mouse coordinate */
  32. char inbyte;
  33. float xfactor, yfactor;         /* Multiply by mouse coords to get the 
  34.                    correct ratio */
  35.  
  36. color pal[256];                 /* Our color palette */
  37. int videomode[10];              /* Array to hold supported modes */
  38. int totalmodes;                 /* Total number of supported VESA modes */
  39.  
  40. char vstring[7][18] = { "640  x 350  x 256",
  41.             "640  x 400  x 256", "640  x 480  x 256", 
  42.             "800  x 600  x 256", "1024 x 768  x 256", 
  43.             "1280 x 1024 x 256", "1600 x 1200 x 256" };
  44.  
  45.  
  46.  
  47.  
  48. void svga_moff (void)
  49. /* Removes the cursor from the screen */
  50. {
  51.  if ((mousestate) && (mousebackground != NULL))
  52.     {
  53.      wvesa_putblock (ox, oy, mousebackground, 0);
  54.      wfreeblock (mousebackground);
  55.      mousebackground = NULL;
  56.     }
  57.   mousestate = 0;
  58. }
  59.  
  60.  
  61. void svga_mon (void)
  62. /* Turns the cursor back on (not drawn until drawmouse) */
  63. {
  64.  mousestate = 1;
  65. }
  66.  
  67.  
  68.  
  69. void svga_drawcursor_retrace (void)
  70. /* Waits for retrace and draws the mouse cursor */
  71. {
  72. int cwidth, cheight;
  73.  
  74.   /* Wait for retrace */
  75.   do {
  76.       inbyte =  inp (0x3DA);
  77.      } while (! (inbyte & 0x08));
  78.  
  79.   if (mousestate)
  80.   {
  81.     if (mousebackground != NULL)
  82.     {
  83.      wvesa_putblock (ox, oy, mousebackground, 0);
  84.      wfreeblock (mousebackground);
  85.      mousebackground = NULL;
  86.     }
  87.   }
  88.  
  89.   ox = (float)mouse.mx * xfactor;
  90.   oy = (float)mouse.my * yfactor;
  91.  
  92.   cwidth = wgetblockwidth (mouse_image[0]) - 1;
  93.   cheight = wgetblockheight (mouse_image[0]) - 1;
  94.  
  95.   if (mousestate)
  96.     {
  97.      mousebackground = wvesa_newblock (ox, oy, ox + cwidth, oy + cheight);
  98.  
  99.      if (mouse_image[0] != NULL)
  100.        wvesa_putblock (ox, oy, mouse_image[0], 1);
  101.     }
  102.  
  103.   do {
  104.       inbyte =  inp (0x3DA);
  105.      } while ((inbyte & 0x08));
  106. }
  107.  
  108.  
  109. void svga_drawcursor (void)
  110. /* Draws the mouse cursor */
  111. {
  112. int cwidth, cheight;
  113.  
  114.   if ((mousestate) && (mousebackground != NULL))
  115.     {
  116.      wvesa_putblock (ox, oy, mousebackground, 0);
  117.      wfreeblock (mousebackground);
  118.      mousebackground = NULL;
  119.     }
  120.  
  121.   cwidth = wgetblockwidth (mouse_image[0]) - 1;
  122.   cheight = wgetblockheight (mouse_image[0]) - 1;
  123.  
  124.   ox = (float)mouse.mx * xfactor;
  125.   oy = (float)mouse.my * yfactor;
  126.  
  127.   if (mousestate)
  128.     {
  129.      mousebackground = wvesa_newblock (ox, oy, ox + cwidth, oy + cwidth);
  130.  
  131.      if (mouse_image[0] != NULL)
  132.        wvesa_putblock (ox, oy, mouse_image[0], 1);
  133.     }
  134. }
  135.  
  136.  
  137. void getmodes (void)
  138. {
  139.   totalmodes = 0;               /* Start counter at 0 modes supported */
  140.  
  141.   /* Now find supported modes and add them to our array */
  142.   if (wvesa_supported (V640x350))
  143.   {
  144.     videomode[totalmodes] = V640x350;
  145.     totalmodes++;
  146.   }
  147.   if (wvesa_supported (V640x400))
  148.   {
  149.     videomode[totalmodes] = V640x400;
  150.     totalmodes++;
  151.   }
  152.   if (wvesa_supported (V640x480))
  153.   {
  154.     videomode[totalmodes] = V640x480;
  155.     totalmodes++;
  156.   }
  157.   if (wvesa_supported (V800x600))
  158.   {
  159.     videomode[totalmodes] = V800x600;
  160.     totalmodes++;
  161.   }
  162.   if (wvesa_supported (V1024x768))
  163.   {
  164.     videomode[totalmodes] = V1024x768;
  165.     totalmodes++;
  166.   }
  167.   if (wvesa_supported (V1280x1024))
  168.   {
  169.     videomode[totalmodes] = V1280x1024;
  170.     totalmodes++;
  171.   }
  172.   if (wvesa_supported (V1600x1200))
  173.   {
  174.     videomode[totalmodes] = V1600x1200;
  175.     totalmodes++;
  176.   }
  177. }
  178.  
  179.  
  180. int which_string (int mode)
  181. {
  182.   /* This function simply returns a string number to display based on the
  183.      highlighted video mode */
  184.   switch (mode)
  185.   {
  186.     case V640x350   : return 0; 
  187.     case V640x400   : return 1; 
  188.     case V640x480   : return 2; 
  189.     case V800x600   : return 3; 
  190.     case V1024x768  : return 4; 
  191.     case V1280x1024 : return 5; 
  192.     case V1600x1200 : return 6; 
  193.   }
  194.   return 0;
  195. }
  196.  
  197.  
  198. int select_mode (void)
  199. {
  200.   int ctr;
  201.   int done;
  202.   int selected;
  203.   struct rccoord endy;
  204.   char ch;
  205.  
  206.   printf ("\nPress ENTER to selected highlighted mode, any other key advances highlight.\n");
  207.  
  208.   for (ctr = 0; ctr < totalmodes; ctr++)        /* Show supported modes */
  209.   {
  210.     _settextposition (13 + ctr, 1);
  211.     _outtext (vstring[ which_string (videomode[ctr]) ]);
  212.   }
  213.   endy = _gettextposition ();
  214.  
  215.   selected = 0;
  216.   done = 0;
  217.   while (!done)
  218.   {
  219.     _settextcolor (12);                       /* Highlight string */
  220.     _settextposition (13 + selected, 1);
  221.     _outtext (vstring[ which_string (videomode[selected]) ]);
  222.     ch = getch ();
  223.     if (ch == 13)                               /* Abort when ENTER pressed */
  224.       done = 1;
  225.     else {
  226.       while (kbhit ()) getch ();  
  227.       _settextcolor (7);                    /* De-highlight previous */
  228.       _settextposition (13 + selected, 1);
  229.       _outtext (vstring[ which_string (videomode[selected]) ]);
  230.       selected++;
  231.       if (selected >= totalmodes)               /* Wrap around list */
  232.     selected = 0;
  233.     }
  234.   }
  235.   _settextposition (endy.row + 1, 1);
  236.  
  237.   return videomode[selected];           /* Return the selected mode */
  238. }
  239.  
  240.  
  241. void main (void)
  242. {
  243. int oldmode;                    /* Video mode before program was started */
  244. int mymode = 0;                 /* Selected video mode */
  245.  
  246.   oldmode = wgetmode ();        /* Preserve our original video mode */
  247.   if (!vgadetected ())
  248.   {
  249.     printf ("VGA is required to run this program...");
  250.     exit (1);
  251.   }
  252.  
  253.   printf ("WGT Example #66\n\n");
  254.   printf ("This program creates a 256 color mouse cursor in SVGA.\n");
  255.   printf ("Press any key to begin.\n");
  256.   getch ();
  257.  
  258.   _clearscreen (_GCLEARSCREEN); /* Clear the screen */
  259.   if (wvesa_detected ())        /* Look for VESA driver */
  260.     printf ("SVGA detected.\n");
  261.   else
  262.   {
  263.     printf ("SVGA support not found. Please check for VESA driver presence.\n");
  264.     exit (1);
  265.   }
  266.  
  267.   /* Display the video card maunfacturer's string */
  268.   printf ("VESA version %x\n", VGA.VESAVersion);
  269.   printf ("VIDEO CARD OEM STRING:\n%s\n", VGA.OEMStringPtr);
  270.   if (VGA.VESAVersion >= 0x200)
  271.   {
  272.     printf ("OEM Software revision #%x\n", VGA.OemSoftwareRev);
  273.     printf ("OEM Vendor Name: %s\n", VGA.OemVendorNamePtr);
  274.     printf ("OEM Product Name: %s\n", VGA.OemProductNamePtr);
  275.     printf ("OEM Product Revision: %s\n", VGA.OemProductRevPtr);
  276.   }
  277.     else printf ("\n\n\n\n");
  278.  
  279.   printf ("Memory on card: %dk\n", VGA.TotalMemory * 64);
  280.  
  281.   getmodes ();                           /* Find supported video modes */
  282.  
  283.   if (totalmodes == 0)
  284.   {
  285.     printf ("256 color SVGA modes not supported. Program aborted.\n");
  286.     exit (1);
  287.   }
  288.   else
  289.   {
  290.     mymode = select_mode ();
  291.   }
  292.  
  293.   if (!wvesa_getmodeinfo (mymode))
  294.     printf ("Mode detection failed.\n");
  295.   else
  296.   {
  297.     printf ("\nMode %x selected.\n\n", mymode);
  298.     printf ("X resolution : %5d\nY resolution : %5d\n", VESAmode.XResolution, VESAmode.YResolution);
  299.     printf ("Banks: %d\n", VESAmode.NumberOfBanks);
  300.     printf ("Window Granularity : %d\n", VESAmode.WinGranularity);
  301.     printf ("Window size in Kb  : %d\n", VESAmode.WinSize);
  302.     if (VESAmode.WinAAttributes & 1 == 0)
  303.       printf("Window A not supported\n");
  304.     else
  305.       printf("Window A Segment   : %x\n", VESAmode.WinASegment);
  306.     if (VESAmode.WinBAttributes & 1 == 0)
  307.       printf("Window B not supported\n");
  308.     else
  309.       printf("Window B Segment   : %x\n", VESAmode.WinBSegment);
  310.   }
  311.  
  312.   printf ("\n\nPRESS ANY KEY TO ENTER GRAPHICS MODE\n");
  313.   getch ();
  314.  
  315.   vga256 ();
  316.   minit ();
  317.   if (!wvesa_init (mymode))
  318.   {
  319.     printf ("Unable to initialize graphics mode.\n");
  320.     exit (1);
  321.   }
  322.  
  323.   /* Set the scaling factors */
  324.   xfactor = (float)VESAmode.XResolution / 320.0;
  325.   yfactor = (float)VESAmode.YResolution / 200.0;
  326.   
  327.  
  328.   /* Load the cursor's image */
  329.   wloadsprites (pal, "svgacurs.spr", mouse_image, 0, 0);
  330.   wsetpalette (0, 255, pal);
  331.  
  332.   msetbounds (0, 0, 
  333.     (VESAmode.XResolution - wgetblockwidth (mouse_image[0]) - 1) / xfactor,
  334.     (VESAmode.YResolution - wgetblockheight (mouse_image[0]) - 1) / yfactor);
  335.   /* Make sure the cursor does not go off the bottom right */
  336.   
  337.   wvesa_cls (0);
  338.  
  339.  
  340.   /* This draw shows the mouse cursor with lines being drawn. */
  341.   do {
  342.  
  343.       /* Wait for retrace */
  344.       do {
  345.      inbyte =  inp (0x3DA);
  346.       } while (! (inbyte & 0x08));
  347.  
  348.       svga_moff ();
  349.       wsetcolor (rand () % 256);
  350.       wvesa_line (0, 0, rand () % VESAmode.XResolution,
  351.           rand () % VESAmode.YResolution);
  352.  
  353.       svga_mon ();
  354.       svga_drawcursor ();
  355.  
  356.        do {
  357.     inbyte =  inp (0x3DA);
  358.        } while ((inbyte & 0x08));
  359.    } while (!kbhit ());
  360.  
  361.  
  362.   while (kbhit ())      /* Clear out the keyboard buffer */
  363.      getch ();
  364.  
  365.   /* This loop shows the mouse cursor with no other drawing going on. */
  366.   do {
  367.       svga_drawcursor_retrace ();
  368.    } while (!kbhit ());
  369.  
  370.   wfreesprites (mouse_image, 0, 0);
  371.  
  372.   
  373.   mdeinit ();
  374.   wsetmode (oldmode);                    /* Return text mode */
  375. }
  376.